package com.hckjsec.oa.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.oa.domain.OaConfig;
import com.hckjsec.oa.service.IOaConfigService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司OA配置Controller
 * 
 * @author cyt
 * @date 2021-12-21
 */
@RestController
@RequestMapping("/oa/config")
public class OaConfigController extends BaseController
{
    @Autowired
    private IOaConfigService oaConfigService;

    /**
     * 查询公司OA配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OaConfig oaConfig)
    {
        startPage();
        List<OaConfig> list = oaConfigService.selectOaConfigList(oaConfig);
        return getDataTable(list);
    }

    /**
     * 获取公司OA配置详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        OaConfig oaConfig = new OaConfig();
        List<OaConfig> list = oaConfigService.selectOaConfigList(null);
        if (!StringUtils.isEmpty(list)){
            oaConfig = list.get(0);
        }
        return AjaxResult.success(oaConfig);
    }


    /**
     * 修改公司OA配置
     */
    @Log(title = "公司OA配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaConfig oaConfig)
    {
        int rows = 0;
        List<OaConfig> list = oaConfigService.selectOaConfigList(null);
        if (StringUtils.isEmpty(list)){
            oaConfig.setDeptId(SecurityUtils.getDeptId());
            oaConfig.setCreateBy(SecurityUtils.getUsername());
            rows =  oaConfigService.insertOaConfig(oaConfig);
        }else{
            Long id = list.get(0).getId();
            oaConfig.setId(id);
            oaConfig.setUpdateBy(SecurityUtils.getUsername());
            rows = oaConfigService.updateOaConfig(oaConfig);
        }
        return toAjax(rows);
    }

}
